package cn.jinjimi.app.main.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.FrameLayout;

import com.baiiu.filter.adapter.MenuAdapter;
import com.baiiu.filter.adapter.SimpleGridTextAdapter;
import com.baiiu.filter.adapter.SimpleTextAdapter;
import com.baiiu.filter.interfaces.OnFilterDoneListener;
import com.baiiu.filter.interfaces.OnFilterItemClickListener;
import com.baiiu.filter.typeview.DoubleListView;
import com.baiiu.filter.typeview.SingleGridView;
import com.baiiu.filter.util.CommonUtil;
import cn.jinjimi.base.util.StringUtils;
import cn.jinjimi.app.R;
import cn.jinjimi.app.entity.Company;
import cn.jinjimi.app.entity.ZdbAge;
import cn.jinjimi.app.entity.ZdbInsurance;
import cn.jinjimi.app.entity.ZdbInsurancesType;
import cn.jinjimi.app.managers.ZdbFilterManager;

import java.util.ArrayList;
import java.util.List;

/**
 * author: baiiu
 * date: on 16/1/17 21:14
 * description:
 */
public class DropMenuAdapter implements MenuAdapter {
    private final Context mContext;
    private OnFilterDoneListener onFilterDoneListener;
    private String[] titles;
    List<ZdbInsurancesType> insuranceListData;
    List<Company> companiesListData;
    List<ZdbAge> zdbAgeListData;

    DoubleListView<ZdbInsurance, ZdbInsurance> comTypeDoubleListView;
    SingleGridView<Company> companySingleGridView;
    SingleGridView<ZdbAge> ageSingleGridView;

    public static final int typeClass = 0;
    public static final int typeCompany = 1;
    public static final int typeAge = 2;

    public DropMenuAdapter(Context context, String[] titles, OnFilterDoneListener onFilterDoneListener) {
        this.mContext = context;
        this.titles = titles;
        this.onFilterDoneListener = onFilterDoneListener;
        this.insuranceListData = ZdbFilterManager.getInstance().getInsuranceListData();
        this.companiesListData = ZdbFilterManager.getInstance().getCompaniesListData();
        this.zdbAgeListData = ZdbFilterManager.getInstance().getZdbAgeListData();
    }

    @Override
    public int getMenuCount() {
        return titles.length;
    }

    @Override
    public String getMenuTitle(int position) {
        return titles[position];
    }

    @Override
    public int getBottomMargin(int position) {
        return 0;
    }

    @Override
    public View getView(int position, FrameLayout parentContainer) {
        View view = parentContainer.getChildAt(position);

        switch (position) {
            case typeCompany:
                view = createCompanySingleGridView();
                break;
            case typeClass:
                view = createDoubleListView();
                break;
            case typeAge:
                view = createAgeSingleGridView();
                break;
        }

        return view;
    }

    @Override
    public void onRefreshView() {

    }

    @Override
    public void indicatorRight(int position, int width) {
//        if (position == typeClass) {
//            setDoubleListFirstRight(width);
//        }
    }

    private View createDoubleListView() {
        comTypeDoubleListView = new DoubleListView<ZdbInsurance, ZdbInsurance>(mContext)
                .leftAdapter(new SimpleTextAdapter<ZdbInsurance>(null, mContext) {
                    @Override
                    public String provideText(ZdbInsurance filterType) {
                        return filterType.getName();
                    }
                })
                .rightAdapter(new SimpleTextAdapter<ZdbInsurance>(null, mContext) {
                    @Override
                    public String provideText(ZdbInsurance s) {
                        return s.getName();
                    }

                })
                .onLeftItemClickListener(new DoubleListView.OnLeftItemClickListener<ZdbInsurance, ZdbInsurance>() {
                    @Override
                    public List<ZdbInsurance> provideRightList(ZdbInsurance item, int position) {
                        List<ZdbInsurance> child = getRightList(position);
                        if (CommonUtil.isEmpty(child)) {
                            String title, id;
                            boolean isFirst = position == 0;
                            title = isFirst ? "险种" : item.getName();
                            id = isFirst ? null : item.getId();
                            onFilterDone(typeClass, title, id, null);
                        }

                        return child;
                    }
                })
                .onRightItemClickListener(new DoubleListView.OnRightItemClickListener<ZdbInsurance, ZdbInsurance>() {
                    @Override
                    public void onRightItemClick(ZdbInsurance item, ZdbInsurance item1) {
                        boolean isFirst = item1.getId() == null;
                        String title = isFirst ? item.getName() : item1.getName();
                        onFilterDone(typeClass, title, item.getId(), item1.getId());
                    }
                });

        //初始化选中.
        comTypeDoubleListView.setLeftList(getLeftList(), 0);
        comTypeDoubleListView.setRightList(getRightList(0), 0);
        comTypeDoubleListView.getLeftListView().setBackgroundColor(ContextCompat.getColor(mContext, R.color.white));
        return comTypeDoubleListView;
    }

    private View createCompanySingleGridView() {
        companySingleGridView = new SingleGridView<Company>(mContext)
                .adapter(new SimpleGridTextAdapter<Company>(null, mContext) {
                    @Override
                    public String provideText(Company s) {
                        return s.getCompanyName();
                    }

                })
                .onItemClick(new OnFilterItemClickListener<Company>() {
                    @Override
                    public void onItemClick(Company item, int position) {
                        String title = position == 0 ? "公司" : item.getCompanyName();
                        onFilterDone(typeCompany, title, item.getCompanyId(), null);
                    }
                });

        companySingleGridView.setList(companiesListData, -1);
        return companySingleGridView;
    }

    private View createAgeSingleGridView() {
        ageSingleGridView = new SingleGridView<ZdbAge>(mContext)
                .adapter(new SimpleGridTextAdapter<ZdbAge>(null, mContext) {
                    @Override
                    public String provideText(ZdbAge zdbAge) {
                        return zdbAge.name;
                    }
                })
                .onItemClick(new OnFilterItemClickListener<ZdbAge>() {
                    @Override
                    public void onItemClick(ZdbAge item, int position) {
                        String title = position == 0 ? "年龄" : item.name;
                        onFilterDone(typeAge, title, item.id, null);
                    }
                });
        ageSingleGridView.setList(zdbAgeListData, -1);
        return ageSingleGridView;
    }

    public void setInsuranceListData(List<ZdbInsurancesType> listData, String subClassCode) {
        if (listData == null)
            listData = new ArrayList<>();
        if (listData.isEmpty() || StringUtils.isNotEmpty(listData.get(0).getId())) {
            ZdbInsurancesType allType = new ZdbInsurancesType();
            allType.setName("全部");
            listData.add(0, allType);
        }

        this.insuranceListData = listData;

        int leftPosition = 0;
        int rightPosition = -1;
        if (StringUtils.isNotEmpty(subClassCode)) {
            for (int i = 0; i < insuranceListData.size(); i++) {
                if (insuranceListData.get(i).getSubclasses() != null) {
                    for (int j = 0; j < insuranceListData.get(i).getSubclasses().size(); j++) {
                        if (subClassCode.equals(insuranceListData.get(i).getSubclasses().get(j).getId())) {
                            leftPosition = i;
                            rightPosition = j;
                        }
                    }
                }
            }
        }
        comTypeDoubleListView.setLeftList(getLeftList(), leftPosition);
        comTypeDoubleListView.setRightList(getRightList(leftPosition), rightPosition + 1);
    }

    public void setCompaniesListData(List<Company> companies, String companyCode) {
        if (companies == null)
            companies = new ArrayList<>();

        if (companies.isEmpty() || StringUtils.isNotEmpty(companies.get(0).getCompanyId())) {
            Company head = new Company(null, "全部");
            companies.add(0, head);
        }

        this.companiesListData = companies;

        int selectPosition = 0;
        if (StringUtils.isNotEmpty(companyCode)) {
            for (int i = 0; i < companiesListData.size(); i++) {
                if (companyCode.equals(companiesListData.get(i).getCompanyId()))
                    selectPosition = i;
            }
        }

        companySingleGridView.setList(companies, selectPosition);
    }

    public void setZdbAgeListData(List<ZdbAge> listData, String ageCode) {
        if (listData == null)
            listData = new ArrayList<>();

        if (listData.isEmpty() || StringUtils.isNotEmpty(listData.get(0).id)) {
            ZdbAge allAge = new ZdbAge();
            allAge.name = "不限年龄";
            listData.add(0, allAge);
        }

        this.zdbAgeListData = listData;

        int selectPostion = 0;
        if (StringUtils.isNotEmpty(ageCode)) {
            for (int i = 0; i < zdbAgeListData.size(); i++) {
                if (ageCode.equals(zdbAgeListData.get(i).id))
                    selectPostion = i;
            }
        }

        ageSingleGridView.setList(listData, selectPostion);
    }

    private List<ZdbInsurance> getLeftList() {
        List<ZdbInsurance> list = new ArrayList<>();
        if (insuranceListData == null || insuranceListData.isEmpty())
            return list;
        for (ZdbInsurancesType temp : insuranceListData) {
            ZdbInsurance item = new ZdbInsurance();
            item.setName(temp.getName());
            item.setId(temp.getId());
            list.add(item);
        }
        return list;
    }


    private List<ZdbInsurance> hasSubclasses(int position) {
        if (insuranceListData != null && !insuranceListData.isEmpty() && position != 0) {
            List<ZdbInsurance> subList = insuranceListData.get(position).getSubclasses();
            if (subList != null && !subList.isEmpty()) {
                if (!subList.get(0).getName().equals("全部")) {
                    ZdbInsurance insurance = new ZdbInsurance();
                    insurance.setName("全部");
                    subList.add(0, insurance);
                }
                return subList;
            }
        }
        return null;
    }

    private List<ZdbInsurance> getRightList(int position) {
        return hasSubclasses(position);
    }


    private void onFilterDone(int position, String title, String type, String subType) {
        if (onFilterDoneListener != null) {
            onFilterDoneListener.onFilterDone(position, title, type, subType);
        }
    }

    public void setDoubleListFirstRight(int i) {
        if (comTypeDoubleListView != null) {
            comTypeDoubleListView.setLeftWidth(i);
        }
    }

}
